package com.braveman.im.ui.activity.message;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.braveman.im.R;
import com.braveman.im.bean.FriendGroupNameBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.adapter.AddFriendsGroupNameAdapter;
import com.braveman.im.ui.weiget.DeleteFromBottomPopup;
import com.braveman.im.ui.weiget.InputPopup;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class AddFriendsGroupNameActivity extends BaseActivity {
    private RecyclerView mLvActivityListMember;
    private LoadingLayout loadingLayout;
    private AddFriendsGroupNameAdapter addFriendsGroupNameAdapter;
    private List<FriendGroupNameBean> list = new ArrayList<>();

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("添加好友分组").addActions(new TitleBarView.Action(R.color.color_19, "完成") {
            @Override
            public void performAction(View view) {
                ActivityUtils.finishActivityTopToBottom(AddFriendsGroupNameActivity.this);
            }
        });
    }

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected int initContentView() {

        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_add_friends_group_name;
    }

    @Override
    protected void initChildView() {
        addFriendsGroupNameAdapter = new AddFriendsGroupNameAdapter(R.layout.item_friends_group_name, list);
        loadingLayout = findViewById(R.id.loading_layout);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(AddFriendsGroupNameActivity.this));

        View headerView = getHeaderView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputPopup inputPopup = new InputPopup(AddFriendsAndGroupActivity.currentActivity);
                inputPopup.showPopupWindow();
            }
        });

        addFriendsGroupNameAdapter.addHeaderView(headerView);
        mLvActivityListMember.setAdapter(addFriendsGroupNameAdapter);
    }

    @Override
    protected void initChildData() {
        doNetWork();
    }

    private void doNetWork() {
        loadingLayout.showLoading();
        ApiManager.getApi().getFriendGroupName().enqueue(new NetCallBack<List<FriendGroupNameBean>>() {
            @Override
            protected void onSuccess(Call<List<FriendGroupNameBean>> call, Response<List<FriendGroupNameBean>> response) {
                if (response != null) {
                    if (list != null && list.size() > 0) {
                        list.clear();
                    }
                    list.addAll(response.body());
                    if (list.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                    if (addFriendsGroupNameAdapter != null) {
                        addFriendsGroupNameAdapter.notifyDataSetChanged();
                    }

                } else {
                    loadingLayout.showError();
                }
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        addFriendsGroupNameAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (list.get(position).ifUndeletable) {
                    UIUtils.showToast("默认分组不能删除");
                    return;
                }


                String id = list.get(position).id;
                DeleteFromBottomPopup deleteFromBottomPopup = new DeleteFromBottomPopup(AddFriendsGroupNameActivity.this, id);
                deleteFromBottomPopup.showPopupWindow();
            }
        });
    }


    private View getHeaderView(int type, View.OnClickListener listener) {
        View view = getLayoutInflater().inflate(R.layout.item_friends_group_name_head, (ViewGroup) mLvActivityListMember.getParent(), false);
        view.setOnClickListener(listener);
        return view;
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        switch (eventCode) {
            case EventCode.EVENT_ADD_GROUP_NAME:
                doNetWork();
                break;
            case EventCode.EVENT_DELETE_GROUP_NAME:
                doNetWork();
                break;
        }
    }
}
